<template>
  <div class="travel-announcement-detail">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>公告详情</span>
          <el-button type="primary" @click="goBack">返回列表</el-button>
        </div>
      </template>

      <!-- 公告详情内容 -->
      <div v-if="announcementDetail" class="announcement-detail-content">
        <!-- 公告标题和元信息 -->
        <div class="announcement-header">
          <div class="announcement-title-wrapper">
            <h1 class="announcement-title">{{ announcementDetail.title }}</h1>
            <el-tag v-if="announcementDetail.isFirst==1" type="warning">置顶</el-tag>
          </div>
          <div class="announcement-meta">
            <span class="publish-time">发布时间：{{ announcementDetail.createTime }}</span>
            <span class="publisher">发布人：{{ announcementDetail.createUser }}</span>
            <span class="view-count">浏览：{{ announcementDetail.viewCount }}</span>
          </div>
        </div>

        <!-- 公告摘要 -->
        <div class="announcement-summary">
          <div class="summary-label">摘要：</div>
          <div class="summary-content">
            {{ announcementDetail.summary}}
          </div>
        </div>

        <!-- 公告正文 -->
        <div class="announcement-body">
            <!-- 实际公告内容 -->
            <div class="content-text" v-html="announcementDetail.content"></div>
        </div>
      </div>

      <!-- 加载中状态 -->
      <div v-else class="loading-state">
        <el-empty description="加载中..." />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { noticeDetail } from '../request/notice.js'



// 路由相关
const route = useRoute()
const router = useRouter()
const announcementId = route.params.id

// 数据
const announcementDetail = ref(null)

// 处理富文本中的图片路径
const processImagePaths = (content) => {
  if (!content) return ''
  
  console.log('原始富文本内容:', content)
  
  // 查找所有img标签以查看原始路径
  const imgTags = content.match(/<img[^>]*>/gi)
  if (imgTags) {
    console.log('找到的img标签数量:', imgTags.length)
    imgTags.forEach(tag => {
      const srcMatch = tag.match(/src=["']([^"']+)["']/)
      if (srcMatch && srcMatch[1]) {
        console.log('原始图片路径:', srcMatch[1])
      }
    })
  }
  
  // 处理图片路径 - 确保所有非绝对路径都走代理转发
  const processedContent = content.replace(/<img\s+[^>]*src=["']([^"']+)["'][^>]*>/gi, (match, src) => {
    // 检查是否已经是绝对路径（http/https/data）
    if (src.startsWith('http://') || src.startsWith('https://') || src.startsWith('data:image/')) {
      console.log('已跳过绝对路径:', src)
      return match
    } else {
      // 根据Vite配置，我们需要为所有相对路径添加/api前缀以走代理转发
      let newSrc = src
      
      // 确保路径格式正确
      if (!src.startsWith('/api')) {
        if (src.startsWith('/')) {
          newSrc = '/api' + src
        } else {
          newSrc = '/api/' + src
        }
      }
      
      console.log('更新图片路径:', src, '->', newSrc)
      return match.replace(src, newSrc)
    }
  })
  
  console.log('处理后的富文本内容:', processedContent)
  return processedContent
}

// 获取公告详情
const getAnnouncementDetail = async () => {
  try {
    // 调用API获取公告详情
    const response = await noticeDetail(announcementId)
    
    // 处理富文本内容中的图片路径
    if (response && response.content) {
      response.content = processImagePaths(response.content)
    }
    
    announcementDetail.value = response
  } catch (error) {
    console.error('获取公告详情失败:', error)
    ElMessage({ message: '获取公告详情失败', type: 'error' })
    router.push('/front/travel-announcement')
  }
}
getAnnouncementDetail();

// 返回列表页
const goBack = () => {
  router.push('/front/travel-announcement')
}
</script>

<style scoped>
.travel-announcement-detail {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.announcement-detail-content {
  margin-top: 20px;
}

.announcement-header {
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
}

.announcement-title-wrapper {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 15px;
}

.announcement-title {
  margin: 0;
  font-size: 24px;
  font-weight: bold;
  color: #333;
  flex: 1;
}

.announcement-meta {
  display: flex;
  gap: 30px;
  font-size: 14px;
  color: #666;
}

.announcement-body {
  padding: 20px 0;
  margin-bottom: 30px;
}

.content-text {
  line-height: 1.8;
  color: #333;
  font-size: 16px;
  white-space: pre-wrap;
}

/* 摘要样式 */
.announcement-summary {
  background-color: #f5f5f5;
  padding: 15px;
  margin: 20px 0;
  border-left: 4px solid #409eff;
  border-radius: 0 4px 4px 0;
}

.summary-label {
  font-weight: bold;
  color: #666;
  margin-bottom: 5px;
}

.summary-content {
  line-height: 1.6;
  color: #333;
  font-size: 15px;
}

/* 富文本内容样式 */
.content-text :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 10px 0;
  border-radius: 4px;
}

.content-text :deep(p) {
  margin-bottom: 15px;
  line-height: 1.8;
}

.content-text :deep(h1),
.content-text :deep(h2),
.content-text :deep(h3) {
  margin: 20px 0 10px;
  font-weight: bold;
}

.content-text :deep(ul),
.content-text :deep(ol) {
  margin: 10px 0 10px 20px;
}

.content-text :deep(li) {
  margin-bottom: 5px;
}

.travel-info-section {
  background-color: #f5f5f5;
  padding: 20px;
  border-radius: 4px;
}

.travel-info-section h3 {
  margin-top: 0;
  margin-bottom: 15px;
  color: #333;
}

.travel-info {
  margin-bottom: 20px;
}

.info-item {
  display: flex;
  margin-bottom: 10px;
}

.info-label {
  width: 100px;
  font-weight: bold;
  color: #333;
}

.info-value {
  flex: 1;
  color: #666;
}

.application-actions {
  margin-top: 20px;
}

.loading-state {
  text-align: center;
  padding: 60px 0;
}
</style>